/* vim: set ts=2 et sw=2 cindent fo=qroca: */
package com.globant.google.mendoza;

import java.util.Date;

import com.globant.google.mendoza.malbec.CalculationListener;
import com.globant.google.mendoza.malbec.GBuy;
import com.globant.google.mendoza.malbec.Order;
import com.globant.google.mendoza.malbec.OrderListener;

import com.globant.google.mendoza.malbec.schema._2.
AuthorizationAmountNotification;
import com.globant.google.mendoza.malbec.schema._2.MerchantCalculationCallback;
import com.globant.google.mendoza.malbec.schema._2.MerchantCalculationResults;
import com.globant.google.mendoza.malbec.schema._2.Money;
import com.globant.google.mendoza.malbec.schema._2.NewOrderNotification;
import com.globant.google.mendoza.malbec.schema._2.RiskInformationNotification;
import com.globant.google.mendoza.malbec.schema._2.ShoppingCart;

import com.globant.google.mendoza.malbec.transport.StandAlone;

/** A sample e-commerce application to use with mendoza.
 */
public final class MerchantApplicationMock {

  /** Private constructor.
   */
  public MerchantApplicationMock() {
    System.out.println("Entering MerchantSampleApplication");
    init();
    System.out.println("Leaving MerchantSampleApplication");
  }

  private void init() {
    /* Create the calculation listener */
    CalculationListener calcListener = new CalculationListener() {

      public MerchantCalculationResults calculate(final Long buyerId,
          final ShoppingCart cart, final String language,
          final MerchantCalculationCallback callback) {
        System.out.println(
            "Merchant Sample App: Merchant calculation callback received");
        return new MerchantCalculationResults();
      };
    };

    /* Create the merchant order listener */
    OrderListener orderListener = new OrderListener() {

      public void receiveMessage(final String command, final String orderNumber,
          final Date timestamp) {
        System.out.println(
            "Merchant Entering receiveMessage('" + command + "', '"
            + orderNumber + "', ...)");
      }

      public void newOrder(final Order order, final Date timestamp,
          final NewOrderNotification newOrderNotification) {
      }

      public void riskInformation(final Order order, final Date timestamp,
          final RiskInformationNotification information) {
      }

      public void stateChanged(final Order order, final Date timestamp,
          final String newFulfillmentState, final String newFinancialState,
          final String oldFulfillmentState, final String oldFinancialState,
          final String reason) {
      }

      public void chargeAmount(final Order order, final Date timestamp,
          final Money latest, final Money total) {
      }

      public void refundAmount(final Order order, final Date timestamp,
          final Money latest, final Money total) {
      }

      public void chargebackAmount(final Order order, final Date timestamp,
          final Money latest, final Money total) {
        System.out.println(
            "Merchant Sample App: Chargeback Amount notification received");
      }

      public void unknown(final String message) {
      }

      public void authorizationAmount(final Order order, final Date timestamp,
          final AuthorizationAmountNotification notification) {
      }
    };

    StandAlone merchantTransport = new StandAlone(null, null,
        MendozaTestConstants.merchantId, MendozaTestConstants.merchantKey,
        MendozaTestConstants.mendozaServerURL,
        MendozaTestConstants.merchantAppMockPort, true);
    GBuy merchantGBuy = new GBuy(merchantTransport);
    merchantGBuy.registerOrderListener(orderListener);
    merchantGBuy.registerCalculationListener(calcListener);
    merchantGBuy.start();

    System.out.println("\nMerchant application mock started...");
  }
}
